home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15800 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.interport.net!scamhi
  2. From: scamhi@interport.net (Steven Camhi)
  3. Newsgroups: comp.lang.c++
  4. Subject: Simple elastic list in C++...
  5. Date: Mon, 08 Apr 96 04:23:25 GMT
  6. Organization: Interport Communications Corp.
  7. Message-ID: <4ka4d5$cia@park.interport.net>
  8. NNTP-Posting-Host: scamhi.port.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=US-ASCII
  11. X-Newsreader: News Xpress 2.0 Beta #0
  12.  
  13. As a programmer old to C, new to C++, I have seen no 'simple' answer to the 
  14. following situation.   In most cases, the easiest, simplest way (for me, that 
  15. is), to create a growable list in straight ANSI C, has been the following:
  16.  
  17. typedef struct MyDataS
  18. .. 
  19. }  MyDataT;
  20.  
  21. void MyFunction()
  22. {
  23.         int allocCount = 0;
  24.         int rowsRead = 0;
  25.         int moreElements;
  26.         MyDataT *listPtr;
  27.  
  28.         for (moreElements = 1; moreElements; rowsRead++)       
  29.         {
  30.                 if (allocCount == rowsRead)
  31.                 {
  32.                         if (allocCount == 0)
  33.                                 listPtr = malloc(sizeof(MyDataT) * 
  34.                                                         (allocCount += 10));   
  35.                       else
  36.                                 listPtr = realloc(listPtr, sizeof(MyDataT) *
  37.                                                         (allocCount += 10));
  38.                         if (listPtr == NULL)
  39.                                 /*
  40.                                 * Do some error handling here.
  41.                                 */
  42.                 }
  43.                 moreElements = ReadAnElement(&listPtr[rowsRead]);
  44.         }
  45. }
  46.  
  47. (Sorry if the syntax is off a little, you get the jist... :)) 
  48.  
  49. Anyway, this model has worked for me because, you can now go ahead and further 
  50. bsearch and qsort away to your hearts content, without fancy 'next pointer' 
  51. fields and all that hassle.  
  52.  
  53. What is *the best* way of doing dynamically growable lists in C++, without a 
  54. headache?  Perhaps there is no *best* way, what is the way to approach the 
  55. problem of manufacturing a simple list class.  One way I have seen is the use 
  56. of the STL vector classes; thats been my frame of reference.  
  57.  
  58. Most books I've read show a simple example of C++ by implementing a stack in 
  59. C++ which inside contains a fixed size array.  This has never been a 'real 
  60. world' example for me.  
  61.  
  62. I've seen some examples on the net use realloc(), and I've heard thats a major 
  63. no-no!  Whats the right way?
  64.  
  65. If this is in a FAQ sorry in advance....
  66.  
  67. TIA
  68. Steve Camhi
  69. scamhi@interport.net
  70.